package com.xjscrm.console.chat.core.impl.conversation;

import cn.hutool.core.util.IdUtil;
import com.uzai.mobile.collect.api.MicaTopics;
import com.uzai.mobile.collect.api.dto.MicaTriggerDto;
import com.uzai.mobile.collect.api.dto.trigger.group.MicaTriggerGroupInviteMsg;
import com.uzai.mobile.collect.api.enums.MicaTriggerType;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RConversationAddMemberMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.entity.ChatTalkSession;
import com.xjscrm.console.mq.producer.SendMsgProducer;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.wwDevice.WwDeviceService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/17 09:42
 */
@Component
@Slf4j
public class ChatConversationAddMemberMsgHandler extends ChatBaseMsgHandler<RConversationAddMemberMsg> {

    @Autowired
    private WwDeviceService wwDeviceService;

    @Autowired
    private SendMsgProducer sendMsgProducer;

    @Autowired
    private ChatTalkSessionService chatTalkSessionService;

    @Override
    public Class<RConversationAddMemberMsg> msgClass() {
        return RConversationAddMemberMsg.class;
    }

    @Override
    public boolean checkMsg(RConversationAddMemberMsg baseMsg) {
        return super.checkMsg(baseMsg) || !ObjectUtils.isNotEmpty(baseMsg.getTalkSessionId()) || !ObjectUtils.isNotEmpty(baseMsg.getWxIds());
    }

    @Override
    public SResultMsg doHandle(RConversationAddMemberMsg content, ChannelHandlerContext ctx) {
        ChatTalkSession byId = chatTalkSessionService.getById(content.getMerId(), content.getTalkSessionId());
        if (Objects.isNull(byId)) {
            throw new BusinessException("会话不存在: 拉人失败");
        }
        MicaTriggerGroupInviteMsg groupInviteMsg = new MicaTriggerGroupInviteMsg();
        groupInviteMsg.setWxIds(byId.getConversationId());
        groupInviteMsg.setWxIds(StringUtils.join(content.getWxIds(), ";"));
        groupInviteMsg.setInviteType(0);

        // 构建trigger对象
        MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(groupInviteMsg, MicaTriggerType.TRIGGER_GROUP_INVITE, item -> {
            item.setReqId(IdUtil.getSnowflakeNextId());
            item.setDeviceId(byId.getDeviceId());
            item.setDeviceType(byId.getDeviceType());
        });
        sendMsgProducer.pushMsg(micaTriggerDto, MicaTopics.WW_TRIGGER_TOPIC);

        return SResultMsg.success(content, "拉人成功");
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_conversation_add_member;
    }
}
